package cn.zyy.hotel.controller.admin;


import cn.zyy.hotel.entity.Carousel;
import cn.zyy.hotel.service.CarouselService;
import cn.zyy.hotel.utils.Result;
import cn.zyy.hotel.utils.SystemConstant;
import cn.zyy.hotel.utils.UUIDUtils;
import cn.zyy.hotel.vo.CarouselVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/carousel")
public class CarouselController {

    @Resource
    private CarouselService carouselService;

    @RequestMapping("/list")
    public Result list(CarouselVO carouselVO){
        //设置分页参数
        PageHelper.startPage(carouselVO.getPage(),carouselVO.getLimit());
        //调用查询方法
        List<Carousel> carousels = carouselService.selectCarouselList();
        //创建分页对象
        PageInfo<Carousel> pageInfo = new PageInfo<>(carousels);
        return Result.builder().code(0).count(pageInfo.getTotal()).data(pageInfo.getList()).build();
    }

    //批量删除轮播图
    @RequiresPermissions("carousel:batch")
    @RequestMapping("/batchDel")
    public Object batchDel(String ids){
        //创建map集合保存数据
        Map<String, Object> map = new HashMap<>();
        //调用批量删除轮播图的方法
        if (carouselService.batchDel(ids) > 0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功");
        }else {
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return map;
    }

    //删除轮播图
    @RequiresPermissions("carousel:delete")
    @RequestMapping("/deleteImg")
    public Object deleteImg(Carousel carousel){
        //创建map集合保存数据
        Map<String, Object> map = new HashMap<>();
        //调用删除轮播图的方法
        if (carouselService.deleteImg(carousel) > 0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功");
        }else {
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return map;
    }

    //轮播图上传
    @RequiresPermissions("carousel:add")
    @RequestMapping("/uploadImg")
    public Object uploadImg(MultipartFile file){
        //创建Map集合保存返回的JSON数据
        Map<String, Object> map = new HashMap<String, Object>();
        //判断是否有选中文件
        if (!file.isEmpty()){
            //获取源文件名
            String oldName = file.getOriginalFilename();
            //获取文件的后缀名
            String extension = StringUtils.getFilenameExtension(oldName);
            //生成新的文件名，防止重名文件
            String newName = UUIDUtils.randomUUID()+"."+extension;
            //为了解决同一个文件夹下文件过多的问题，使用日期作为文件夹管理
            String datePath = "carousels";
            //组装最后的文件名
            String fileName = datePath+"\\"+newName;
            Carousel carousel = Carousel.builder().imgName(oldName).uuidName(fileName).createDate(new Date()).build();
            //创建文件对象
            File img = new File(SystemConstant.IMAGE_UPLOAD_PATH, fileName);
            try {
                //文件上传
                file.transferTo(img);
                //状态码
                map.put(SystemConstant.SUCCESS,true);
                map.put(SystemConstant.MESSAGE,"添加成功");
                carouselService.addCarousel(carousel);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return map;
    }



}
